//
//  UIScrollView+HHEmpty.swift
//  BCScrollViewKit
//
//  Created by chun.chen on 2021/8/26.
//  空页面

import HHNetworkKit

public class Empty: NSObject {
    public let scrollView: UIScrollView

    // MARK: - init

    init(scrollView: UIScrollView) {
        self.scrollView = scrollView
    }

    /// 是否加载数据成功
    public var success = true

    /// 空页面标题
    public var title = "暂时没有内容"

    /// 空页面数据
    public var image = BCScrollEmptyDataImage()

    /// 空页面按钮
    public var buttonTitle: String?

    /// 按钮点击回调
    public var didTapButtonHandle: (() -> Void)?

    /// 添加空数据显示
    /// - Parameters:
    ///   - title: title description
    ///   - image: image description
    /// - Returns: description
    @discardableResult
    public func addEmpty(title: String = "暂时没有内容", image: UIImage? = BCScrollEmptyDataImage()) -> Self {
        self.title = title
        self.image = image
        scrollView.emptyDataSetSource = self
        scrollView.emptyDataSetDelegate = self
        return self
    }

    /// 添加按钮
    /// - Parameter buttonTitle: 按钮标题
    /// - Returns: description
    @discardableResult
    public func buttonTitle(_ buttonTitle: String?) -> Self {
        self.buttonTitle = buttonTitle
        return self
    }

    /// 添加按钮点击事件
    /// - Parameter closure: 逃逸闭包
    /// - Returns: description

    public func didTapButton(_ closure: @escaping () -> Void) -> Self {
        didTapButtonHandle = closure
        return self
    }

    // MARK: - network

    public func isNoNetwork() -> Bool {
        return !ZHReachabilityService.sharedInstance().isAvailable()
    }

    /// 按钮标题预置
    /// - Returns: description
    public func prepareButtonTitle() -> String {
        guard isNoNetwork() else {
            return "重新连接"
        }
        guard success else {
            return "重新获取"
        }
        return buttonTitle ?? ""
    }
}

extension Empty: DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {
    public func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
        return BCScrollEmptyDataTitle(title)
    }

    public func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
        return image
    }

    public func buttonTitle(forEmptyDataSet scrollView: UIScrollView, for state: UIControl.State) -> NSAttributedString? {
        guard let _ = didTapButtonHandle else {
            return nil
        }
        return NSAttributedString(string: prepareButtonTitle(), attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14)])
    }

    public func emptyDataSet(_ scrollView: UIScrollView, didTap button: UIButton) {
        didTapButtonHandle?()
    }

    public func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView) -> Bool {
        return true
    }

    public func emptyDataSetDidAppear(_ scrollView: UIScrollView) {
    }
}

private var scrollEmptyKey: Void?
public extension UIScrollView {
    // MARK: - property

//    public var empty: Empty {
//        return Empty(scrollView: self)
//    }

    private(set) var empty: Empty {
        get {
            if let empty_ = objc_getAssociatedObject(self, &scrollEmptyKey) as? Empty {
                return empty_
            } else {
                let tmpEmpty = Empty(scrollView: self)
                objc_setAssociatedObject(self, &scrollEmptyKey, tmpEmpty, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
                return tmpEmpty
            }
        }
        set {
            objc_setAssociatedObject(self, &scrollEmptyKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
}
